package com.groupfour.attendancemanagementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.dao.TeacherDao;
import com.groupfour.attendancemanagementsystem.entity.DO.TeacherDO;
import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherAddDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherFindDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherUpdateDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.EmploymentStatusAndTeacherCode;
import com.groupfour.attendancemanagementsystem.entity.VO.TeacherVO;
import com.groupfour.attendancemanagementsystem.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("teacherServiceImpl")
public class TeacherServiceImpl implements TeacherService {

    private TeacherDao teacherDao;

    @Autowired
    public TeacherServiceImpl(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    /**
     * @author： 卑微小峰
     * @param： userCode
     * @return： int
     * @exception: none
     * @describe： 获取教师登录信息
     */
    @Override
    public TeacherDO getTeacherByTeacherCode(String userCode) {
        return teacherDao.getTeacherByTeacherCode(userCode);
    }

    /**
     * @author： 卑微小峰
     * @param： newPw,userCode
     * @return： int
     * @exception: none
     * @describe： 教师修改自己的密码
     */
    @Override
    public int updatePassword(String newPwd, String userCode) {
        return teacherDao.updatePassword(newPwd,userCode);
    }


    @Override
    public Map<String, Object> findTeacherByTeacherFindDTO(TeacherFindDTO teacherFindDTO, Integer pageNum, Integer pageSize) {

        // 创建Map存储返回值
        Map<String,Object> map = new HashMap<>();


        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);

        //查询结果
        List<TeacherVO> teacherByTeacherFindDTO = teacherDao.findTeacherByTeacherFindDTO(teacherFindDTO);

        //创建分页信息对象
        PageInfo<TeacherVO> pageInfo = new PageInfo<>(teacherByTeacherFindDTO);

        //将结果返回
        List<TeacherVO> result = pageInfo.getList();

        List<TeacherVO> resultVO = new ArrayList<>();

        for (TeacherVO teacherVO : result) {
            TeacherVO vo = new TeacherVO();
            BeanUtils.copyProperties(teacherVO, vo);
            resultVO.add(vo);
        }
        map.put("pageCount",pageInfo.getTotal());
        map.put("result",resultVO);
        return map;
    }


    @Override
    public int AddTeacher(TeacherAddDTO teacherAddDTO) {
        int flag;
        String teaCode=teacherAddDTO.getTeacherCode();
        int is = teacherDao.selectTeacherCode(teaCode);
        if(is > 0){
            return flag = 2;
        }else {
            System.out.println("=======");
            int bool = teacherDao.AddTeacher(teacherAddDTO);
            if (bool > 0) {
                return flag = 3;
            }
            return flag = 1;
        }
    }

    /**
     * @author li
     * @param teacherUpdateDTO
     * @return int
     * @describe 修改教师基本信息
     */
    @Override
    public int UpdateTeacher(TeacherUpdateDTO teacherUpdateDTO) {
        return teacherDao.UpdateTeacher(teacherUpdateDTO);
    }

    /**
     * @author li
     * @param employmentStatusAndTeacherCode
     * @return boolean
     * @describe 修改教师状态
     */
    @Override
    public int UpdateTeacherEmploymentStatus(EmploymentStatusAndTeacherCode employmentStatusAndTeacherCode) {
        return teacherDao.UpdateTeacherEmploymentStatus(employmentStatusAndTeacherCode);
    }
}
